using System;
using System.Windows.Forms;

namespace ThirdLife.UI.WinForms
{
  public class MainScreenUIModule : IUIModule
  {
    private MainScreen _mainScreen;
    private IUIModuleServices _moduleServices;

    #region IUIModule Members

    public IUIModuleServices ModuleServices
    {
      get { return _moduleServices; }
      set { _moduleServices = value; }
    }

    public string Name
    {
      get { return "ThirdLife_MainScreen"; }
    }

    public void Setup()
    {
      _mainScreen = new MainScreen(this);
      _mainScreen.SetupView();
      ModuleServices.Startup.ApplicationStarted += Startup_ApplicationStarted;
      ModuleServices.Registration.RequestAddToMainScreen += Registration_RequestAddToMainScreen;
    }

    #endregion

    private void Startup_ApplicationStarted()
    {
      ModuleServices.Registration.AddMenu("File",0);
      //we probably always want this to be last
      ModuleServices.Registration.AddMenuItem("File", "Quit", 10000, new EventHandler(OnQuitButtonPressed));
      Application.Run(_mainScreen);
    }

    private void OnQuitButtonPressed(object sender, EventArgs e)
    {
      Application.Exit();
    }

    private void Registration_RequestAddToMainScreen(object control, object location)
    {
      _mainScreen.AddDockableControl(control as IDockableUserControlView, location as IDockLocation);
    }

    public void WindowIsClosing()
    {
      ModuleServices.Metaverse.RequestDisconnectionFromMetaverse();
    }
  }
}